package swa2012.telco.provider;

import java.util.Collection;

import swa2012.telco.broker.Broker;
import swa2012.telco.broker.beans.*;
import javax.jws.WebService;
/**
 * @author kumar aberer
 * @date 10.06.2012
 */


@WebService(endpointInterface = "swa2012.telco.provider.ClientProvider")
public class ClientProviderImpl implements ClientProvider {

	private static Broker broker;
	private static TelcoProvider provider;
	private static MessageStorage msgs;
	private static ClientStorage clients;
	
	public ClientProviderImpl(){
		//provider = new TelcoProvider();
		
	}
	
	public ClientProviderImpl(TelcoProvider provider, MessageStorage msgs, ClientStorage clients)
	{
		
		this.provider = provider;
		this.msgs = msgs;
		this.clients = clients;
		
	}
	@Override
	public Client register(String clientID) {
		Client c = new Client(clientID,provider.getProvider());
		this.clients.addClient(c);
		System.out.println("registering new client: " + c.getID());
		return c;
	}

	@Override
	public void deregister(String clientID) {
		System.out.println("deregister: " + clientID);
		clients.removeClient(clientID);
		
	}

	@Override
	public Collection<Message> getMessages(String clientID) {
		return msgs.getMessages(clientID);
	}

	@Override
	public void sendMessage(Message msg) {
		if(clients.containsClient(msg.getReceiver().getID()))
			msgs.addMessage(msg);
		else
			this.provider.getBroker().sendMessage(msg);
		System.out.println("trying to send message '" + msg.getText() + "' to: " + msg.getReceiver().getID() + "......");
		
	}

	@Override
	public Collection<Client> listClients() {
		return this.provider.getBroker().listClients();
	}

}